﻿using KTD.Web.BLL;
using KTD.Web.Models;
using KTD.Web.Models.VD;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XXS.Common;
using XXS.Common.ControllerExt;

namespace KTD.Web.Controllers
{
    public class DicController : Controller
    {

        public List<SelectListItem> desList = new List<SelectListItem>() {
            new SelectListItem(){Text=DicType.楼盘发布类型.ToString(),Value="0"},
            new SelectListItem(){Text=DicType.新盘业态.ToString(),Value="1"},
            new SelectListItem(){Text=DicType.租售业态.ToString(),Value="2"},
            new SelectListItem(){Text=DicType.大宗物业业态.ToString(),Value="3" },
            new SelectListItem(){Text=DicType.价格.ToString(),Value="4"}
        };
        // GET: Dic
        public ActionResult Index(DicQD im)
        {
            if (im.DicType == null) im.DicType = DicType.楼盘发布类型;
            ViewBag.desList = desList;
            ViewBag.DicType = im.DicType;
            var res = MF.DicManager.GetListBy(im);
            res.Action = "Index";
            res.ControllerContext = this.ControllerContext;
            return View(res);
        }

        public ActionResult Detail(int Id)
        {

            ViewBag.Id = Id;
            var res = MF.DicManager.Find(Id);
            return View(res);
        }


        [HttpPost]
        public ActionResult Create(DicCUD im)
        {

            MF.DicManager.Create(im);
            return this.JsonNet(new { succuess = true });
        }

        [HttpGet]
        public ActionResult Create(DicType Type)
        {
            var model = new DicCUD();
            model.DicType = Type;
            return View(model);
        }



        [HttpGet]
        public ActionResult Update(int Id)
        {
            var model = MF.DicManager.Find(Id);
            return View(model);
        }


        public ActionResult Delete(int Id)
        {
            try
            {
                MF.DicManager.Delete(Id);
                return this.JsonNet(new { success = true });
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message });
            }
        }


        public ActionResult Find(int Id)
        {
            try
            {
                var res = MF.DicManager.Find(Id);
                return this.JsonNet(new { success = true, data = res }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        [HttpPost]
        public ActionResult Update(DicCUD im)
        {
            try
            {
                MF.DicManager.Update(im);
                return this.JsonNet(new { success = true });
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message });
            }
        }

        public ActionResult GetList()
        {
            try
            {
                var res = MF.DicManager.GetListBy(new DicQD() { PageSize = 40 });
                return this.JsonNet(res.PageData.ToExtGrid(), JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        [OutputCache(Duration = 60 * 60, VaryByParam = "*")]
        public ActionResult GetNewDicList(int cityId)
        {
            try
            {
                var areas = MF.AreaManager.GetListBy(new AreaQD() { PageSize = 40,ParentId=cityId }).PageData.OrderByDescending(n => n.sort).Select(n => new SelectVD { Value = n.name,Title=n.name, Order = n.sort }).ToList();
                var prices = MF.DicManager.GetListBy(new DicQD() { PageSize = 40,DicType=DicType.价格 }).PageData.ToList();
                var type = MF.DicManager.GetListBy(new DicQD() { PageSize = 40, DicType = DicType.新盘业态 }).PageData.ToList();
                areas.Insert(0, new SelectVD { Title = "区域", Value = "" });
                prices.Insert(0, new DicVD {  Value = "不限" });
                type.Insert(0, new DicVD {  Value = "业态" });
                return this.JsonNet(new { success = true, areas, prices, type }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }
        [OutputCache(Duration = 60 * 60, VaryByParam = "*")]
        public ActionResult GetSaleDicList(int? cityId)
        {
            try
            {
                var areas = MF.AreaManager.GetListBy(new AreaQD() { PageSize = 40, ParentId = cityId }).PageData.OrderByDescending(n => n.sort).Select(n => new SelectVD { Value = n.name, Title = n.name, Order = n.sort }).ToList();
                var type = MF.DicManager.GetListBy(new DicQD() { PageSize = 40, DicType = DicType.租售业态 }).PageData.ToList();
                areas.Insert(0, new SelectVD { Title = "区域", Value = "" });
                type.Insert(0, new DicVD { Value = "业态" });
                return this.JsonNet(new { success = true, areas, type }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }
        [OutputCache(Duration = 60 * 60, VaryByParam = "*")]
        public ActionResult GetBigDicList(int? cityId)
        {
            try
            {
                var areas = MF.AreaManager.GetListBy(new AreaQD() { PageSize = 40, ParentId = cityId }).PageData.OrderByDescending(n => n.sort).Select(n => new SelectVD { Value = n.name, Title = n.name, Order = n.sort }).ToList();
                var prices = MF.DicManager.GetListBy(new DicQD() { PageSize = 40, DicType = DicType.价格 }).PageData.ToList();
                var type = MF.DicManager.GetListBy(new DicQD() { PageSize = 40, DicType = DicType.大宗物业业态 }).PageData.ToList();
                areas.Insert(0, new SelectVD { Title = "区域", Value = "" });
                prices.Insert(0, new DicVD { Value = "不限" });
                type.Insert(0, new DicVD { Value = "业态" });
                return this.JsonNet(new { success = true, areas, prices, type }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        [OutputCache(Duration = 60 * 60, VaryByParam = "*")]
        public ActionResult GetAllDicList(int? cityId)
        {
            try
            {
                var citys = MF.AreaManager.GetListBy(new AreaQD() { PageSize = 100, isSon = "false" }).PageData.OrderBy(n => n.sort).Select(n => new SelectVD { Value = n.name, Title = n.name, Order = n.sort }).ToList();
                var areas = MF.AreaManager.GetListBy(new AreaQD() { PageSize = 100, isSon="true"}).PageData.OrderByDescending(n => n.sort).Select(n => new SelectVD { Value = n.name, Title = n.name, Order = n.sort }).ToList();
                var prices = MF.DicManager.GetListBy(new DicQD() { PageSize = 40, DicType = DicType.价格 }).PageData;
                var BigTypes = MF.DicManager.GetListBy(new DicQD() { PageSize = 40, DicType = DicType.大宗物业业态 }).PageData;
                var NewTypes = MF.DicManager.GetListBy(new DicQD() { PageSize = 40, DicType = DicType.新盘业态 }).PageData;
                var SaleTypes = MF.DicManager.GetListBy(new DicQD() { PageSize = 40, DicType = DicType.租售业态 }).PageData;
                var PublishTypes = MF.DicManager.GetListBy(new DicQD() { PageSize = 40, DicType = DicType.楼盘发布类型 }).PageData;
                return this.JsonNet(new { success = true, citys, areas, prices, BigTypes, NewTypes, SaleTypes, PublishTypes }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }
    }
}